﻿// IniFile.cpp: implementation of the CIniFile class.
//
//////////////////////////////////////////////////////////////////////
#pragma once
#include "pch.h"
#include "IniFile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif

#define MAX_LENGTH      1024  //ַռ䳤
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIniFile::CIniFile() :
	IniFileName(_T(""))
{

}

CIniFile::~CIniFile()
{

}


CString CIniFile::GetString(CString AppName, CString KeyName, CString Default)
{
	CString sTring = _T("");
	::GetPrivateProfileString(AppName, KeyName, Default,
		sTring.GetBuffer(MAX_LENGTH), MAX_LENGTH, IniFileName);
	sTring.ReleaseBuffer();
	return sTring;
}


BOOL CIniFile::Delete(CString AppName, CString KeyName)
{
	return ::WritePrivateProfileString(AppName, KeyName, NULL, IniFileName);
}


int CIniFile::GetInt(CString AppName, CString KeyName, int Default)
{
	return ::GetPrivateProfileInt(AppName, KeyName, Default, IniFileName);
}

DOUBLE  CIniFile::GetDouble(CString AppName, CString KeyName, double Default)
{
	CString sTemp, sTring;
	sTemp.Format(_T("%u"), Default);
	::GetPrivateProfileString(AppName, KeyName, sTemp,
		sTring.GetBuffer(MAX_LENGTH), MAX_LENGTH, IniFileName);
	sTring.ReleaseBuffer();
	return _ttof(sTring);
}

DWORD CIniFile::GetDword(CString AppName, CString KeyName, unsigned long Default)
{
	CString sTemp, sTring;
	sTemp.Format(_T("%u"), Default);
	::GetPrivateProfileString(AppName, KeyName, sTemp,
		sTring.GetBuffer(MAX_LENGTH), MAX_LENGTH, IniFileName);
	sTring.ReleaseBuffer();
	return _ttol(sTring);
}

BOOL CIniFile::SetString(CString AppName, CString KeyName, CString Data)
{
	return ::WritePrivateProfileString(AppName, KeyName, Data, IniFileName);
}

BOOL CIniFile::SetInt(CString AppName, CString KeyName, int Data)
{
	CString temp;
	temp.Format(_T("%d"), Data);
	return ::WritePrivateProfileString(AppName, KeyName, temp, IniFileName);
}

BOOL CIniFile::SetDouble(CString AppName, CString KeyName, double Data)
{
	CString temp;
	temp.Format(_T("%f"), Data);
	return ::WritePrivateProfileString(AppName, KeyName, temp, IniFileName);
}

BOOL CIniFile::SetDword(CString AppName, CString KeyName, unsigned long Data)
{
	CString temp;
	temp.Format(_T("%u"), Data);
	return ::WritePrivateProfileString(AppName, KeyName, temp, IniFileName);
}

INT CIniFile::GetSections(CStringArray& sectionArray)
{
	DWORD nRecodeValue = 0;
	TCHAR  szMaxBufer[4096] = { 0 };

	sectionArray.RemoveAll();
	DWORD nSize = GetPrivateProfileSectionNames(szMaxBufer, 4096, IniFileName);

	for (DWORD index = 0; index < nSize; index++)
	{
		if (szMaxBufer[index] == 0)
		{
			sectionArray.Add(szMaxBufer + nRecodeValue);
			nRecodeValue = index + 1;
		}
	}
	return sectionArray.GetSize();
}

INT CIniFile::GetSectionKeys(CString AppName, CStringArray& keyArray, UINT nType)
{
	DWORD nRecodeValue = 0;
	TCHAR  szMaxBufer[4096] = { 0 };
	keyArray.RemoveAll();
	DWORD nSize = GetPrivateProfileSection(AppName, szMaxBufer, 4096, IniFileName);

	for (DWORD index = 0; index < nSize; index++)
	{
		if (szMaxBufer[index] == 0)
		{
			CString LineContent = szMaxBufer + nRecodeValue;
			switch (nType)
			{
			case KEY_INFO:
				if (LineContent.Find(_T("=")) >= 0)
					LineContent = LineContent.Left(LineContent.Find(_T("=")));
				else
					LineContent.Empty();
				break;
			case VAL_INFO:
				if (LineContent.Find(_T("=")) >= 0)
					LineContent = LineContent.Mid(LineContent.Find(_T("=")) + 1);
				else
					LineContent.Empty();
				break;
			case  ALL_INFO:default:
				break;
			}
			if (LineContent.IsEmpty() == FALSE)
				keyArray.Add(LineContent);
			nRecodeValue = index + 1;
		}
	}
	return keyArray.GetSize();
}